// Copyright 2008 mateamargo

//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at

//        http://www.apache.org/licenses/LICENSE-2.0

//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

package btb.core;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import btb.core.collisions.PlayerToBeattleCollision;
import btb.core.sprites.BatmanSprite;

import com.golden.gamedev.Game;
import com.golden.gamedev.object.Background;
import com.golden.gamedev.object.CollisionManager;
import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.object.SpriteGroup;
import com.golden.gamedev.object.background.ImageBackground;
import com.golden.gamedev.util.ImageUtil;

/**
 * This class is responsible of the Sprites creation and game initialization.
 * It also has access to the current user input state.
 * @author mateamargo
 *
 */
public class BtbGame extends Game {
	
	private Level currentLevel;
	
	private BatmanSprite batman;
	
	private SpriteGroup playerGroup;
	
	private SpriteGroup beatlesGroup;
	
	private Background bg;
	
	private CollisionManager collision;
	
	private List<Sprite> beatles;
	
	double batmanStep = 0.2;
	
	double beatleStep = 0.2;
	
	final int LEFT = 0;
	
	final int RIGHT = 1;
	
	int direction = LEFT;
	
	int topRange = 100;
	
	int beatleTopRange = 200;
	
	int bottomRange = 300;
	
	int leftRange = 20;
	
	int rightRange = 500;
	
	int beatleSpawnRate = 1000; // 2 seconds
	
	int elapsedSum = 0;	
	
	public int getTopRange() {
		return topRange;
	}

	public void setTopRange(int topRange) {
		this.topRange = topRange;
	}

	public int getBottomRange() {
		return bottomRange;
	}

	public void setBottomRange(int bottomRange) {
		this.bottomRange = bottomRange;
	}

	public int getLeftRange() {
		return leftRange;
	}

	public void setLeftRange(int leftRange) {
		this.leftRange = leftRange;
	}

	public int getRightRange() {
		return rightRange;
	}

	public void setRightRange(int rightRange) {
		this.rightRange = rightRange;
	}
	
	String beatleImg = "/home/mateamargo/Projects/Games/lwjgl-game/prueba/src/res/beatle-transparency.png";
	BufferedImage btbi;

	@Override
	public void initResources() {
		String bg1 = "/home/mateamargo/Projects/Games/lwjgl-game/prueba/src/res/background1.png";
		bg = new ImageBackground(getImage(bg1));
		
		btbi = ImageUtil.getImage(bsIO.getURL(beatleImg), Transparency.TRANSLUCENT);
		
		batman = new BatmanSprite(this, 300, 100);
		
		playerGroup = new SpriteGroup("Player");
		playerGroup.setBackground(bg);
		playerGroup.add(batman);
		
		beatlesGroup = new SpriteGroup("Beatles");
		beatlesGroup.setBackground(bg);
		
		collision = new PlayerToBeattleCollision();
		collision.setCollisionGroup(playerGroup, beatlesGroup);
		
		setMaskColor(Color.BLACK);
		
		beatles = new ArrayList<Sprite>();
		
	}

	@Override
	public void render(Graphics2D g) {
		bg.render(g);
		beatlesGroup.render(g);
		playerGroup.render(g);
	}
	
	@Override
	public void update(long elapsedTime) {
		bg.update(elapsedTime);
		playerGroup.update(elapsedTime);
		beatlesGroup.update(elapsedTime);
		
		collision.checkCollision();

		checkPlayerMovent(elapsedTime);
	}
	
	private void checkPlayerMovent(long elapsedTime) {
		elapsedSum += elapsedTime;
		
		if(elapsedSum >= beatleSpawnRate){
			createBeatle();
			elapsedSum = 0;
		}
		
		batman.doLogic(elapsedTime);
		
		for(int i = 0; i < beatles.size(); i++){
			beatles.get(i).move(beatleStep * elapsedTime, 0);
			
			if(beatles.get(i).getX() >= rightRange){
				beatles.get(i).setActive(false);
				beatles.remove(beatles.get(i));
				
				System.out.println("Removing the beatle");
			}
		}
	}
	
	private void createBeatle(){
		// Always starts with the leftRange coordinate and calculate randomly the y.
		
		Random r = new Random();
		int y = r.nextInt(bottomRange);
		
		if(y < beatleTopRange){
			y += beatleTopRange;
		}
		
		System.out.println("Top range: " + topRange + ", current y: " + y);
		
		Sprite beatle = new Sprite(btbi, leftRange, y);
		beatlesGroup.add(beatle);
		
		beatle.setImage(ImageUtil.rotate(btbi, 90));
		
		beatles.add(beatle);
	}

	public List<Sprite> getBeatles() {
		return beatles;
	}

	public void setBeatles(List<Sprite> beatles) {
		this.beatles = beatles;
	}

	public void notifyDeath(){
		System.out.println("Batman is dead");
		notifyExit();
	}
	
	public void notifyLevelFinished(){
		System.out.println("Level finished");
	}

	public Level getCurrentLevel() {
		return currentLevel;
	}

	public void setCurrentLevel(Level currentLevel) {
		this.currentLevel = currentLevel;
	}
}

